<template>
  <div>
    <div v-if="theImageList.length > 0" style="text-align: center;">
      <el-badge
        v-for="(image, index) in theImageList"
        :key="index"
        :is-dot="image[urlProp] === theCurrentImageUrl"
        style="margin-right: 20px;"
      >
        <el-image
          style="width: 120px; height: 120px"
          fit="contain"
          :src="image[urlProp] ? image[urlProp].indexOf('http') >= 0 ? image[urlProp] : resUrl + image[urlProp] : ''"
          @click.native="setCurrentImage(image)"
        ></el-image>
      </el-badge>
    </div>
    <div v-else>暂无图片供选择</div>
  </div>
</template>

<script>
import * as auth from '@/utils/auth'
import { doPostJson } from '@/utils/utils'
const resUrl = process.env.VUE_APP_RES_URL

export default {
  name: 'ImageChoose',
  props: {
    imageList: {
      type: Array,
      default: () => []
    },
    urlProp: {
      type: String,
      default: 'picUrl'
    },
    currentImageUrl: {
      type: Number,
      default: 0
    },
    searchPicUrl: {
      type: String,
      defalut: null
    }
  },
  data() {
    return {
      resUrl,
      theImageList: this.imageList,
      theCurrentImageUrl: this.currentImageUrl,
      theSearchPicUrl: this.searchPicUrl
    }
  },
  computed: {},
  mounted() {},
  methods: {
    initImages(currentImageUrl, picSearchParams) {
      this.theCurrentImageUrl = currentImageUrl
      this.theImageList = []
      doPostJson(this.theSearchPicUrl, picSearchParams, {})
        .then(response => {
          response.data.rows.forEach(pic => {
            this.theImageList.push(pic)
          })
          this.theCurrentImageUrl = currentImageUrl
        })
        .catch(error => {})
    },
    setCurrentImage(image) {
      this.theCurrentImageUrl = image.picUrl
      this.$emit('setCurrentImage', image)
    }
  }
}
</script>

<style lang="scss">
</style>
